import Field from './field';
import Validator from './../core/validate';
/**
 * 输入框字段类
 */
export default class InputField extends Field {
    constructor(options) {
        super(options);
        this.disabled = options.disabled;
        this.formatter = options.formatter;
        this.nativeType = options.nativeType;
        this.placeholder = options.placeholder;
        if (options.validate) this.validate = new Validator(this, options);
    }
    bindEvent() {
        const inputEl = this.$el.children('input').get(0);
        const spanEl = this.$el.children('span').get(0);
        const keyupHandle = (event) => {
            const newValue = event.target.value;
            const { prop, value: oldValue, rowIndex } = this;
            // 更新当前字段数值
            this.value = newValue;
            // 更新data数据值
            this.table.data[rowIndex][prop] = newValue;
            // 更新合计值
            this.table.updateSummaryData();
            // 触发全局事件钩子
            this.table.event.onFieldChange(rowIndex, prop, oldValue, newValue);
        }
        const blurHandle = (event) => this.validate.verification(event.target.value).catch(() => { });
        this.validate && inputEl.addEventListener('blur', blurHandle);
        inputEl.addEventListener('keyup', keyupHandle);
        this.keyupHandle = keyupHandle;
        this.blurHandle = blurHandle;
        this.inputEl = inputEl;
        this.spanEl = spanEl;
    }
    removeEvent() {
        const { validate, inputEl, keyupHandle, blurHandle } = this;
        validate && inputEl.removeEventListener('blur', blurHandle);
        inputEl.removeEventListener('keyup', keyupHandle);
    }
    // 生成验证
    generateValidateWarn(message) {
        this.spanEl.innerText = message;
        message ? $(this.inputEl).addClass('border') : $(this.inputEl).removeClass('border');
    }
    setSpecifiedItem(newValue) {
        const { rowIndex, prop } = this;
        // 更新当前字段数值
        this.value = newValue;
        // 更新dom值
        this.inputEl.value = newValue;
        // 更新data数据值
        this.table.data[rowIndex][prop] = newValue;
    }
}